/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CriarPerfil2d.java
 *
 * Created on 18/09/2011, 19:16:24
 */
package visao;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JOptionPane;
import modelo.objeto2d.Aresta2d;
import modelo.objeto2d.Objeto2d;

/**
 *
 * @author TyTu
 */
public class CriarPerfil2d extends javax.swing.JFrame {

    /** Creates new form CriarPerfil2d */
    public CriarPerfil2d() {
        initComponents();
    }
    private Point p = new Point();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jToolBar1 = new javax.swing.JToolBar();
        jRadioButtonDesenhar = new javax.swing.JRadioButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jRadioButtonMover = new javax.swing.JRadioButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jRadioButtonEscala = new javax.swing.JRadioButton();
        jSeparator6 = new javax.swing.JToolBar.Separator();
        jRadioButtonRotacionar = new javax.swing.JRadioButton();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jSeparator9 = new javax.swing.JToolBar.Separator();
        jButtonRevolucao = new javax.swing.JButton();
        jButtonExtrusao = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jSeparator10 = new javax.swing.JToolBar.Separator();
        jButtonLimpar = new javax.swing.JButton();
        jSeparator8 = new javax.swing.JToolBar.Separator();
        jButtonCancelar = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        jPanel1 = new javax.swing.JPanel();

        buttonGroup1.add(jRadioButtonDesenhar);
        buttonGroup1.add(jRadioButtonMover);
        buttonGroup1.add(jRadioButtonRotacionar);
        buttonGroup1.add(jRadioButtonEscala);
        jRadioButtonDesenhar.setSelected(true);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jToolBar1.setRollover(true);

        jRadioButtonDesenhar.setText("Desenhar");
        jRadioButtonDesenhar.setFocusable(false);
        jRadioButtonDesenhar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonDesenhar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jRadioButtonDesenhar);
        jToolBar1.add(jSeparator3);

        jRadioButtonMover.setText("Mover");
        jRadioButtonMover.setEnabled(false);
        jRadioButtonMover.setFocusable(false);
        jRadioButtonMover.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonMover.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonMover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMoverActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonMover);
        jToolBar1.add(jSeparator5);

        jRadioButtonEscala.setText("Escala");
        jRadioButtonEscala.setEnabled(false);
        jRadioButtonEscala.setFocusable(false);
        jRadioButtonEscala.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonEscala.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonEscala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonEscalaActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonEscala);
        jToolBar1.add(jSeparator6);

        jRadioButtonRotacionar.setText("Rotacionar");
        jRadioButtonRotacionar.setEnabled(false);
        jRadioButtonRotacionar.setFocusable(false);
        jRadioButtonRotacionar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonRotacionar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonRotacionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonRotacionarActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonRotacionar);
        jToolBar1.add(jSeparator7);
        jToolBar1.add(jSeparator1);
        jToolBar1.add(jSeparator9);

        jButtonRevolucao.setText("Revolução");
        jButtonRevolucao.setEnabled(false);
        jButtonRevolucao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRevolucaoActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonRevolucao);

        jButtonExtrusao.setText("Extrusão");
        jButtonExtrusao.setEnabled(false);
        jButtonExtrusao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExtrusaoActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonExtrusao);
        jToolBar1.add(jSeparator2);
        jToolBar1.add(jSeparator10);

        jButtonLimpar.setText("Limpar");
        jButtonLimpar.setFocusable(false);
        jButtonLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimparActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonLimpar);
        jToolBar1.add(jSeparator8);

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonCancelar);
        jToolBar1.add(jSeparator4);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "X Y", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanel1MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanel1MouseReleased(evt);
            }
        });
        jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanel1MouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 609, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 464, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseClicked
        // TODO add your handling code here:

        if (jRadioButtonDesenhar.isSelected()) {
            controlador.Controle.addPonto2d(evt.getX(), evt.getY());
            System.out.println("Criar Objeto 2d");
            System.out.println("X= " + evt.getX() + " Y= " + evt.getY());
        }



    }//GEN-LAST:event_jPanel1MouseClicked

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseDragged
        // TODO add your handling code here:     
        Point p1 = evt.getPoint();
        double dx = (double) p1.x - p.x;
        double dy = (double) p1.y - p.y;
        //if (controlador.Controle.getPerfil2d().isOnFaces(new Ponto2d(p1.x, p1.y))) {
            if (jRadioButtonMover.isSelected()) {
                controlador.Controle.getPerfil2d().translacao(dx, dy);
                p = p1;
                desenha();
            } else if (jRadioButtonEscala.isSelected()) {
                controlador.Controle.getPerfil2d().escala((dx / 100) + 1, (dy / 100) + 1);
                p = p1;
                desenha();
            } else if (jRadioButtonRotacionar.isSelected()) {
                double angulo = (dx - controlador.Controle.getPerfil2d().getCentro().getX())
                        / (dy - controlador.Controle.getPerfil2d().getCentro().getY());

                controlador.Controle.getPerfil2d().rotaciona(angulo);

                p = p1;
                desenha();
            }
        //}
    }//GEN-LAST:event_jPanel1MouseDragged

    private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanel1MousePressed

    private void jPanel1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseReleased
        // TODO add your handling code here:
//        Point p1 = evt.getPoint();
//        if (jRadioButtonMover.isSelected()) {
//            controlador.Controle.getPerfil2d().translacao((double) p1.x - p.x, (double) p1.y - p.y);
//            vai = false;
//        }
//        if (jRadioButtonEscala.isSelected()) {
//
//            controlador.Controle.getPerfil2d().escala((double) (p1.x - p.x) / p1.x, (double) (p1.y - p.y) / p1.y);
//
//            vai = false;
//        }
//        if (jRadioButtonRotacionar.isSelected()) {
//            double angulo = (p1.x - controlador.Controle.getPerfil2d().getCentro().getX())
//                    / (p1.y - controlador.Controle.getPerfil2d().getCentro().getY());
//            controlador.Controle.getPerfil2d().rotaciona(angulo);
//            vai = false;
//        }
//        desenha();
    }//GEN-LAST:event_jPanel1MouseReleased

    private void jButtonRevolucaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRevolucaoActionPerformed
        // TODO add your handling code here:
        int grid = -1;

        String s;
        for (;;) {
            try {
                s = JOptionPane.showInputDialog(rootPane, "Grid: ");
                if (s != null) {
                    grid = Integer.parseInt(s);
                    if (grid > 0) {
                        break;
                    }
                }else{
                    break;
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "Valor digitado incorreto!");
                e.printStackTrace();
            }
        }
        if (s != null && grid > 0) {
            controlador.Controle.revolucao(grid);
            dispose();
        }
    }//GEN-LAST:event_jButtonRevolucaoActionPerformed

    private void jButtonExtrusaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExtrusaoActionPerformed
        // TODO add your handling code here:
        int z = -1;

        String s;
        for (;;) {
            try {
                s = JOptionPane.showInputDialog(rootPane, "Variação em z: ");
                if (s != null) {
                    z = Integer.parseInt(s);
                    break;
                }else{
                    break;
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "Valor digitado incorreto!");
                e.printStackTrace();
            }
        }
        if (s != null) {

            controlador.Controle.extrusao(z);

            dispose();
        }
    }//GEN-LAST:event_jButtonExtrusaoActionPerformed

    private void jRadioButtonMoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMoverActionPerformed
        // TODO add your handling code here:
        jRadioButtonDesenhar.setEnabled(false);
    }//GEN-LAST:event_jRadioButtonMoverActionPerformed

    private void jRadioButtonEscalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonEscalaActionPerformed
        // TODO add your handling code here:
        jRadioButtonDesenhar.setEnabled(false);
    }//GEN-LAST:event_jRadioButtonEscalaActionPerformed

    private void jRadioButtonRotacionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonRotacionarActionPerformed
        // TODO add your handling code here:
        jRadioButtonDesenhar.setEnabled(false);
    }//GEN-LAST:event_jRadioButtonRotacionarActionPerformed

    private void jButtonLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimparActionPerformed
        // TODO add your handling code here:
        controlador.Controle.LimpaPerfil2d();
        jButtonExtrusao.setEnabled(false);
        jButtonRevolucao.setEnabled(false);
        jRadioButtonEscala.setEnabled(false);
        jRadioButtonMover.setEnabled(false);
        jRadioButtonRotacionar.setEnabled(false);
        jRadioButtonDesenhar.setEnabled(true);
        jRadioButtonDesenhar.setSelected(true);
        desenha();
    }//GEN-LAST:event_jButtonLimparActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CriarPerfil2d().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonExtrusao;
    private javax.swing.JButton jButtonLimpar;
    private javax.swing.JButton jButtonRevolucao;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButtonDesenhar;
    private javax.swing.JRadioButton jRadioButtonEscala;
    private javax.swing.JRadioButton jRadioButtonMover;
    private javax.swing.JRadioButton jRadioButtonRotacionar;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator10;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar.Separator jSeparator6;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JToolBar.Separator jSeparator8;
    private javax.swing.JToolBar.Separator jSeparator9;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    public void desenha() {
        try {
            Objeto2d o = controlador.Controle.getPerfil2d();
            if (!jButtonExtrusao.isEnabled() && !o.isEmpy()) {
                jButtonExtrusao.setEnabled(true);
                jButtonRevolucao.setEnabled(true);
                jRadioButtonEscala.setEnabled(true);
                jRadioButtonMover.setEnabled(true);
                jRadioButtonRotacionar.setEnabled(true);
            }
            Graphics g = jPanel1.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, jPanel1.getWidth(), jPanel1.getHeight());
            g.setColor(Color.BLACK);
            for (Aresta2d aresta : o.getArestas()) {
                g.drawLine(
                        (int) aresta.getP1().getX(),
                        (int) aresta.getP1().getY(),
                        (int) aresta.getP2().getX(),
                        (int) aresta.getP2().getY());
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        desenha();
    }
}
